{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From https://juliabyexample.helpmanual.io/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# function to calculate the volume of a sphere\n",
    "function sphere_vol(r)\n",
    "    # julia allows Unicode names (in UTF-8 encoding)\n",
    "    # so either \"pi\" or the symbol π can be used\n",
    "    return 4/3*pi*r^3\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# functions can also be defined more succinctly\n",
    "quadratic(a, sqr_term, b) = (-b + sqr_term) / 2a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculates x for 0 = a*x^2+b*x+c, arguments types can be defined in function definitions\n",
    "function quadratic2(a::Float64, b::Float64, c::Float64)\n",
    "    # unlike other languages 2a is equivalent to 2*a\n",
    "    # a^2 is used instead of a**2 or pow(a,2)\n",
    "    sqr_term = sqrt(b^2-4a*c)\n",
    "    r1 = quadratic(a, sqr_term, b)\n",
    "    r2 = quadratic(a, -sqr_term, b)\n",
    "    # multiple values can be returned from a function using tuples\n",
    "    # if the return keyword is omitted, the last term is returned\n",
    "    r1, r2\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vol = sphere_vol(3)\n",
    "# @printf allows number formatting but does not automatically append the \\n to statements, see below\n",
    "@printf \"volume = %0.3f\\n\" vol\n",
    "#> volume = 113.097"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "quad1, quad2 = quadratic2(2.0, -2.0, -12.0)\n",
    "println(\"result 1: \", quad1)\n",
    "#> result 1: 3.0\n",
    "println(\"result 2: \", quad2)\n",
    "#> result 2: -2.0"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "encoding": "# -*- coding: utf-8 -*-",
   "main_language": "julia",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}